<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <PageTools>
          <template #after>
            <el-button type="primary" @click="addbtn">添加角色</el-button>
          </template>
        </PageTools>
        <el-table :data="tableList" border>
          <el-table-column label="角色名称" prop="title" />
          <el-table-column label="角色描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button type="primary" @click="permbtn(row.id)">分配权限</el-button>
              <el-button @click="editbtn(row.id)">编辑</el-button>
              <el-button type="danger" @click="delbtn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加和编辑的弹窗组件 -->
      <addRoles ref="addRoles" :is-show.sync="isShow" @updata="loadrolesList" />
      <!-- 分配权限 -->
      <permList ref="permList" :show-dialog.sync="showDialog" />
    </div>
  </div>

</template>

<script>
import addRoles from './components/addRoles.vue'
import permList from './components/permList.vue'
import { rolesList, delRoles } from '@/api/roles'
export default {
  components: {
    addRoles,
    permList
  },
  data() {
    return {
      showDialog: false, // 分配权限的显示和隐藏
      isShow: false, // 添加框的显示和隐藏
      tableList: [
        {
          title: '角色名称',
          description: '角色描述'
        }
      ]
    }
  },
  created() {
    this.loadrolesList()
  },
  methods: {
    // 分配权限
    permbtn(id) {
      this.showDialog = true
      this.$refs.permList.getprems(id)
    },
    // 删除角色
    delbtn(id) {
      this.$confirm('您确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除业务
        // 发送请求
        await delRoles(id)
        // console.log(res)
        // 渲染页面
        this.loadrolesList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑角色按钮
    editbtn(id) {
      this.isShow = true
      // console.log(id)
      // 数据回显
      this.$refs.addRoles.editbtn(id)
    },
    // 添加角色按钮
    addbtn() {
      this.isShow = true
    },
    async loadrolesList() {
      const res = await rolesList({
        _limit: 100,
        _start: 1
      })
      // console.log(res.data)
      this.tableList = res.data
    }
  }
}
</script>

<style>
</style>
